/**
 * Created by dell on 2018/12/10.
 */
/*
   登录验证（前端+后端+数据库）
 */

const express = require('express');
const bodyParser = require('body-parser');
const db = require('./db.js');
const app = express();

//挂载参数处理中间件（post   需要body-parser中间件）
app.use(bodyParser.urlencoded({extended:false}));

//启用静态资源服务器
app.use(express.static('public'));
//路由器
app.post('/check',(req,res)=>{
    let param = req.body;
    // console.log('para >>> '  + param)

    let sql = 'select count(*) as total from user where username=? and password=?';
    let data = [param.username,param.password];
    db.base(sql,data,(result)=>{
        if(result[0].total == 1){

            res.send('login success!');
        } else{
            res.send('login failure!');
        }
    });
});

let server = app.listen(3000,()=>{
    var host = server.address().address;
    var port = server.address().port;

    console.log("应用实例，访问地址为 http://%s:%s", host, port)

});